package com.config;

import com.controller.FirstInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@ServletComponentScan(basePackages = {"com.filter","com.Listener"})

public class MyServletMvcConfiguration implements WebMvcConfigurer {
    @Autowired
    private FirstInterceptor firstInterceptor;
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //第一种写法（类上面没有扫描用的注解）的时候的注册方法
        //registry.addInterceptor(new FirstInterceptor());

        //第二种方法就是不需要new一个拦截器对象了
        registry.addInterceptor(firstInterceptor)
                .order(Ordered.HIGHEST_PRECEDENCE)
                .excludePathPatterns("/login")
                .excludePathPatterns("/static/**");
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowCredentials(true)
                .allowedMethods("GET", "POST", "DELETE", "PUT", "OPTIONS", "HEAD")
                .allowedOrigins("http://127.0.0.1.5500");

    }
}
